﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;

namespace ECare.Client.frms
{
    public partial class frmOuts : frmGridTemplate
    {
        public frmOuts()
        {
            InitializeComponent();
            this.Load += frmOuts_Load;

            this.grd.CellDoubleClick += grd_CellDoubleClick;


            this.grd.CellFormatting += grd_CellFormatting;

        }

        void grd_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < grd.Rows.Count)
            {
                if (grd.Columns[e.ColumnIndex].Name == "plan_out_check_status")
                {
                    var currentstatus = grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

                    if (currentstatus == CheckStatus.Qualitified.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (currentstatus == CheckStatus.Rejected.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (currentstatus == CheckStatus.UnFinished.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
                    }
                    else
                    {

                    }


                }
            }
        }

        void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as PlanOutInfo;
            var frm = new frmOutInfoReadOnly();
            frm.entity = entitye;
            frm.ShowDialog(); 
            
        }
        public BindingList<PlanOutInfo> source { get; set; }
        void frmOuts_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            source = new BindingList<PlanOutInfo>(PlanHelper.GetPlanOuts());
            this.grd.DataSource = source;

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;

            var entity = this.grd.SelectedRows[0].DataBoundItem as PlanOutInfo;
            var frm = new frmOutInfoCheck {entity = entity};
            frm.ShowDialog();

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;

            var entity = this.grd.SelectedRows[0].DataBoundItem as PlanOutInfo;

            if (entity.plan_isout)
            {
                MessageBox.Show("已经生成出库单");
                return;
            }
            
            var frm = new frmOutFormInfo { entity = entity };
            frm.ShowDialog();

           

        }
    }
}
